import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class LifeCycleCase extends StatefulWidget {
  const LifeCycleCase({super.key});
  @override
  // ignore: no_logic_in_create_state
  State createState() {
    // 组件创建是调用
    debugPrint('createState');
    return _LifeCycleCase();
  }
}

class _LifeCycleCase extends State<LifeCycleCase> {
  int count = 0;
  @override
  void initState() {
    if (kDebugMode) {
      // 组件初始化时调用 用于发起网络请求 组件变量赋值 更新状态
      print('initState');
    }
    super.initState();
  }

  @override
  void didChangeDependencies() {
    if (kDebugMode) {
      print('didChangeDependencies');
    }
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(covariant LifeCycleCase oldWidget) {
    if (kDebugMode) {
      print('didUpdateWidget');
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void deactivate() {
    if (kDebugMode) {
      print('deactivate');
    }
    super.deactivate();
  }

  @override
  void dispose() {
    if (kDebugMode) {
      print('dispose');
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      height: 200,
      color: Colors.red,
      child: Column(
        children: [
          Text('组件生命周期: ${count.toString()}'),
          TextButton(
              onPressed: () {
                setState(() {
                  count++;
                });
              },
              child: const Text('增加'))
        ],
      ),
    );
  }
}
